<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: base.class.php 29558 2016-06-20 11:29:39Z mpage $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

class plugin_dzapp_base {
	
	function plugin_dzapp_base() {
		global $_G;

		$this->appid = $_G['setting']['dzapp_base_appid'];
		$this->appsecret = $_G['setting']['dzapp_base_appsecret'];
		$this->debug = $_G['setting']['dzapp_base_debug'];
	}

	function global_usernav_extra1(){
		global $_G;

		if($_G['adminid'] != 1) return;

		return '<span class="pipe">|</span><a href="javascript:;" id="dzapp_base" class="showmenu" onmouseover="showMenu(this.id)">DZAPP</a> ';
	}

	function global_footer(){
		global $_G;

		if($_G['adminid'] != 1) return;

		$return .= '<ul class="p_pop" id="dzapp_base_menu" style="display: none">';
		$query = C::t('common_plugin')->fetch_all_data();
		foreach($query as $value) {
			if(strpos($value['identifier'], 'dzapp_') !== 0 || !in_array($value['identifier'], array('dzapp_base', 'dzapp_zhe'/*, 'dzapp_alipay', 'dzapp_wxpay', 'dzapp_tenpay', 'dzapp_paypal'*/))) continue;
			$return .= '<li><a href="plugin.php?id='.$value['identifier'].'&mod='.str_replace('dzapp_', '', $value['identifier']).'cp">'.str_replace('DZAPP', '', $value['name']).'</a></li>';
		}
		$return .= '</ul>';
		return $return;
	}
}

class mobileplugin_dzapp_base {

	function mobileplugin_dzapp_base() {
		global $_G;

		$this->appid = $_G['setting']['dzapp_base_appid'];
		$this->appsecret = $_G['setting']['dzapp_base_appsecret'];
		$this->debug = $_G['setting']['dzapp_base_debug'];
	}

	function global_footer_mobile() {
		global $_G;

		loadcache('dzapp_base_jssdk');
		if($_GET['mod'] != 'viewthread') {
			$mod = str_replace('dzapp_', '', $_GET['id']).'_'.$_GET['mod'];
		}

		if(empty($_G['cache']['dzapp_base_jssdk'][$mod]['available'])) {
			return;
		}

		$signPackage = $this->getSignPackage();
		include template('dzapp_base:base_jssdk');
		return $return;
	}

	function getSignPackage() {

		$jsapiTicket = $this->getJsApiTicket();
		$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
		$timestamp = time();
		$nonceStr = $this->createNonceStr();

		$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
		$signature = sha1($string);

		$signPackage = array(
			"appId"     => $this->appid,
			"nonceStr"  => $nonceStr,
			"timestamp" => $timestamp,
			"url"       => $url,
			"signature" => $signature,
			"rawString" => $string
		);
		return $signPackage; 
	}

	function createNonceStr($length = 16) {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$str = "";
		for ($i = 0; $i < $length; $i++) {
			$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
		}
		return $str;
	}

	function getJsApiTicket() {
		global $_G;

		loadcache('jsapi_ticket');
		$data = $_G['cache']['jsapi_ticket'];
		if ($data['expire_time'] < time()) {
			$accessToken = $this->getAccessToken();
			$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
			$res = json_decode(get($url));
			$ticket = $res->ticket;
			if ($ticket) {
				$data['expire_time'] = time() + 7000;
				$data['jsapi_ticket'] = $ticket;
				savecache('jsapi_ticket', $data);
			}
		} else {
			$ticket = $data['jsapi_ticket'];
		}

		return $ticket;
	}

	function getAccessToken() {
		global $_G;

		loadcache('access_token');
		$data = $_G['cache']['access_token'];
		if ($data['expire_time'] < time()) {
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appsecret";
			$res = json_decode(get($url));
			$access_token = $res->access_token;
			if ($access_token) {
				$data['expire_time'] = time() + 7000;
				$data['access_token'] = $access_token;
				savecache('access_token', $data);
			}
		} else {
			$access_token = $data['access_token'];
		}

		return $access_token;
	}
}

?>